package com.example.Controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.Service.UserService;
import com.example.entity.Users;
import com.example.entity.vo.UsersVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.github.pagehelper.page.PageMethod.startPage;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    UserService userService;
    @PostMapping("/selectAll")
    public SaResult selectAll(@RequestBody  UsersVo uservo) {
        PageInfo<UsersVo> user = userService.selectAll(uservo);
        if(user!=null){
            SaResult res=SaResult.data(user);
            return res;
        }else{
            return SaResult.error("查询失败");
        }
    }

//    /user/deletById
    @PostMapping("/deletById")
    public SaResult deletById(@RequestBody UsersVo uservo  ) {
     System.out.println(uservo.getUserId()+"userId");
    int i = userService.deletById(uservo.getUserId());
    if(i>0){
        return SaResult.ok("删除成功");
    }else{
        return SaResult.error("删除失败");
    }
    }
    @PostMapping("/insert")
    public SaResult insert(@RequestBody  Users user  ) {
        int i = userService.insert(user);
        if(i>0){
            return SaResult.ok("新增成功");
        }else{
            return SaResult.error("新增失败");
        }
    }
    @PostMapping("/update")
    public SaResult update(@RequestBody  Users user) {
        int i = userService.update(user);
        if(i>0){
            return SaResult.ok("编辑成功");
        }else{
            return SaResult.error("编辑失败");
        }
    }
}
